<!--基础数据页-->
<template>
  <div style="background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">报销类型</span>
    <el-button  type="primary" style="float:right;margin-top:13px;">返回</el-button>
  </div>
  <!-- 字典项搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="报销名称">
        <el-input placeholder="请输入" style="width:220px;"  v-model="searchReimbursementForm.name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button>重置</el-button>
        <el-button type="primary" @click="loadReimbursement">搜索</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card style="margin:20px;">
    <div style="margin:10px 0px 40px 10px;">
      报销类型
      <el-button type="primary" style="float: right;margin-top:-3px;" @click="addAuditType" >新增报销</el-button>
    </div>
    <el-table :data="reimbursementArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="name" label="类别名称" align="center"/>
      <el-table-column prop="applicableDepartment" label="适用部门" align="center"
                       :formatter="departmentTypeFormatter"></el-table-column>
      <el-table-column prop="status" label="状态" align="center"
                       :formatter="statusTypeFormatter"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary"
                     @click="editReimbursement(scope.row.id)">编辑</el-button>
          <el-button link type="primary"
                     @click="deleteReimbursement(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 新增费用类型弹窗 -->
  <el-dialog
      :title="dialogTitle"
      style="width: 1000px;padding: 40px;"
      v-model="dialogVisible"
      :before-close="handleClose"
  >
    <el-form label-position="top" label-width="80px">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="费用名称">
            <el-input placeholder="请输入" v-model="saveReimbursementForm.name" @change="loadReimbursement"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="应用部门">
            <el-select placeholder="请选择" v-model="saveReimbursementForm.applicableDepartment" @change="loadReimbursement">
              <el-option v-for="item in departmentTypeArr"
                         :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="状态">
            <el-radio-group v-model="saveReimbursementForm.status" @change="loadReimbursement">
              <el-radio border label="启用" value="1" style="margin:0;"></el-radio>
              <el-radio border label="禁用" value="0"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveReimbursement">保存</el-button>
    </template>
  </el-dialog>
</template>
<script setup>


//定义变量控制弹窗是否出现
import {onMounted, ref} from "vue";
import axios from "axios";
import qs from "qs";
import {ElMessage} from "element-plus";

const dialogVisible = ref(false);
//定义变量用来控制弹窗的标题
const dialogTitle = ref('新建审批流程');
// 用户列表
const users = ref(getUser());
//定义对象用来收集保存表单的数据
const saveReimbursementForm = ref({
  name:'',
  applicableDepartment:'',
  status:'',
});
const addAuditType = ()=>{
  dialogVisible.value = true;
  dialogTitle.value = '新建费用类型';
}
//定义保存用户的方法
const saveReimbursement = ()=>{
  console.log(saveReimbursementForm.value);
  /* 安装查询字符串框架qs  npm install qs
  使用qs框架将JS对象转为查询字符串格式 name=tom&age=18,注意qs需要导入!!! import qs from "qs"; */
  let data = qs.stringify(saveReimbursementForm.value);
  console.log(data);
  //向后端发送保存员工的请求
  /* 此处传的参数是qs转换后的查询字符串data,注意:使用了查询字符串,后端Controller不用加@RequestBody注解!!! */
  axios.post(BASE_URL+'/oa1/reimbursement/save',data)
      .then((response)=>{
        if(response.data.code == 2000){
          ElMessage.success('保存成功!');
          //关闭弹窗,并清空双向绑定的表单数据
          dialogVisible.value = false;
          saveReimbursementForm.value = {};
          loadReimbursement();
          //保存成功后要重新加载用户数据!
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}
//处理弹窗关闭的方法
const handleClose = ()=>{
  if(confirm('您确认要关闭本窗口吗?')){
    //关闭弹窗,并清空双向绑定的表单数据
    dialogVisible.value = false;
    saveReimbursementForm.value = {};
  }
}
//定义对象用来保存查询条件
const searchReimbursementForm = ref({name:''});
//定义对象保存查询到的表格数据
const reimbursementArr = ref([]);
//定义加载用户的方法
const loadReimbursement = ()=>{
  let data = qs.stringify(searchReimbursementForm.value);//username=tom&status=1
  //如果有查询条件,就根据后面拼接的条件查,没有条件默认加载所有用户数据
  axios.get(BASE_URL+'/oa1/reimbursement/select?'+data)
      .then((response)=>{//回调函数中处理响应结果
        if(response.data.code == 2000){
          reimbursementArr.value = response.data.data;//将后端响应数据装入用户数组中
          console.log(reimbursementArr.value);
        }else{
          ElMessage.error(response.data.msg);
        }
      })
}
//定义重置搜索条件的方法
const resetSearch = ()=>{
  //清空之前的搜索条件
  searchReimbursementForm.value = {};
  //重新加载所有的用户数据
  loadReimbursement();
}

/* 页面一加载完毕,立即执行 */
onMounted(()=>{
  loadReimbursement();
})

//创建修改审批流程的选项
//创建删除员工的方法
const deleteReimbursement = (id)=>{
  if(confirm('您确认要删除该数据吗?')){
    axios.post(BASE_URL+'/oa1/reimbursement/delete/'+id)
        .then((response)=>{
          if(response.data.code == 2000){
            ElMessage.success('删除成功!');
            loadReimbursement();
          }else{
            ElMessage.error(response.data.msg);
          }
        })
  }
}
//定义部门类型列表
const departmentTypeArr = ref([]);
const departmentTypeFormatter = (row,column,cellValue,index)=>{
  if(!departmentTypeArr.value){
    return cellValue;
  }
  for(let item of departmentTypeArr.value){
    if(item.value == cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}
//定义状态
const statusTypeArr = ref([]);
const statusTypeFormatter = (row,column,cellValue,index)=>{
  if(!statusTypeArr.value){
    return cellValue;
  }
  for(let item of statusTypeArr.value){
    if(item.value == cellValue){
      cellValue = item.label;
    }
  }
  return cellValue;
}

onMounted(()=>{
  loadDictOption(departmentTypeArr,'department_type');
  loadDictOption(statusTypeArr,'general_status');
})
const editReimbursement = (id)=>{
  console.log(id)
  dialogVisible.value = true;
  dialogTitle.value = '修改费用信息';
  axios.get(BASE_URL+'/oa1/reimbursement/select?id='+id).then((response)=>{
    if(response.data.code == 2000){
      saveReimbursementForm.value = response.data.data[0];
      dialogVisible.value = true;
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
</script>
<style scoped>

</style>